<?php

namespace app\model\store;

use app\base\BaseModel;
use think\model\concern\SoftDelete;

class StoreArticleModel extends BaseModel
{
    use SoftDelete;

    protected $name = 'store_article';

    /**
     * 范围查询-keyword
     * 
     * @param [type] $query
     * @param [type] $keyword
     * @return void
     */
    public function scopeKeyword($query,$keyword)
    {
        if($keyword != null){
            $query->where('a.title','like',"%{$keyword}%");
        }
    }

    /**
     * 范围查询-store_id
     * 
     * @param [type] $query
     * @param [type] $store_id
     * @return void
     */
    public function scopeStore($query,$store_id)
    {
        if($store_id != null){
            $query->where('a.store_id',$store_id);
        }
    }

    /**
     * 范围查询-status
     *
     * @param [type] $query
     * @param [type] $status
     * @return void
     */
    public function scopeStatus($query,$status)
    {
        if($status != null){
            $query->where('a.status',$status);
        }
    }

    /**
     * 范围查询-is_hot
     *
     * @param [type] $query
     * @param [type] $is_hot
     * @return void
     */
    public function scopeHot($query,$is_hot)
    {
        if($is_hot != null){
            $query->where('a.is_hot',$is_hot);
        }
    }

    /**
     * 获取分页
     *
     * @param [type] $store_id
     * @param [type] $keyword
     * @param [type] $status
     * @param [type] $is_hot
     * @param [type] $current_page
     * @param [type] $list_rows
     * @return void
     */
    public function getPageData($store_id,$keyword,$status,$is_hot,$current_page,$list_rows)
    {
        $dataList = $this
                ->alias('a')
                ->field('a.id,a.create_time,a.title,a.desc,a.is_hot,a.tag,a.status,b.store_name,count(c.id) as like_count')
                ->join('t_store b','a.store_id=b.id')
                ->join('t_store_article_like c','a.id=c.article_id','left')
                ->keyword($keyword)
                ->store($store_id)
                ->status($status)
                ->hot($is_hot)
                ->group('a.id')
                ->order('is_hot desc,create_time desc')
                ->paginate(['page' => $current_page, 'list_rows' => $list_rows], false);
        $current = $dataList->currentPage();
        $total = $dataList->total();
        $limit = $dataList->listRows();
        $items = $dataList->items();
        $ret = ['total' => $total, 'current' => $current, 'limit' => $limit, 'dataList' => $items];
        return $ret;
    }
}